<?php

	if(!array_key_exists("fname", $_POST) || !array_key_exists("lname", $_POST) || !array_key_exists("email", $_POST)){
		header("Location: /index.php");
		exit;
	}
	
	include_once("../class/db.php");
	include_once("../class/login.php");
	$db = new db();
	$login = new login();
	$fname = preg_replace("/[^a-zA-Z0-9_\-\.\s]/", "", $_POST['fname']);
	$lname = preg_replace("/[^a-zA-Z0-9_\-\.\s]/", "", $_POST['lname']);
	$email = preg_replace("/[^a-zA-Z0-9_@\-\.\s]/", "", $_POST['email']);
	$type = $_POST['type'];
	
	//Random password generator
	$random_possibilities = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	$password_clean = "";
	for($i = 0; $i < 8; $i++ ){
		$password_clean .= $random_possibilities[rand(1, strlen($random_possibilities) - 1)];
	}
	$password = sha1($password_clean);
	
	$sql = "INSERT INTO users (password, email, account, fname, lname) values ('$password', '$email', $type, '$fname', '$lname')";
	$db->execute($sql, "none");
	
	$sql = "SELECT username FROM users WHERE email = '$email'";
	$username = $db->execute($sql, "onerow");
	$valid = $login->loginCheck($username['username'], $password);
	
	//Send the email with the information
	$mensaje = '<table width="750" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td><img src="/images/email-header.png" alt="Welcome aboard" width="750" height="100" border="0" usemap="#Map" /></td>
      </tr>
      <tr>
        <td><p>Hi <strong>'.$fname.' '.$lname.'</strong>;</p>
          <p>We are happy to have you aboard and we want to welcome you. This email contain most of the information you\'ll need to access to our system, please keep this email safe. After your first login we recommend to change your password and keep your personal information updated.</p>
          <p><strong>username:</strong> '.$username['username'].'<br />
            <strong>password:</strong> '.$password_clean.'</p>
          <p>If you have any problems, remember we have a support section where you can find all the help and instructions you may need to access, use and learn in our platform. We\'ll always be ready to help you with any question or doubt you can have, just contact us.</p>
          <p><h2>Some helpful links:</h2></p>
          <p><strong>Support Center</strong><br />
          <a href="http://www.pie.edu/help/main.php" target="_blank">http://www.pie.edu/help/main.php</a></p>
          <p><strong>Questions or comments</strong><br />
          <a href="http://www.pie.edu/common/contact-us.php" target="_blank">http://www.pie.edu/common/contact-us.php</a></p>
          <p><strong>Site home</strong><br />
          <a href="http://www.pie.edu/" target="_blank">http://www.pie.edu/</a></p>
          <p>Recover Password<br />
          <a href="http://www.pie.edu/recover.php" target="_blank">http://www.pie.edu/recover.php</a></p></td>
      </tr>
      <tr>
        <td><img src="/images/email-footer.png" alt="Follow us on Facebook, Google+ and Twitter" width="750" height="100" border="0" usemap="#Map2" /></td>
      </tr>
    </table>
	<map name="Map" id="Map">
	  <area shape="rect" coords="527,39,587,67" href="http://www.pie.edu/store" target="_blank" alt="Visit our store" />
	  <area shape="rect" coords="595,38,668,70" href="http://www.pie.edu/network" target="_blank" alt="Talk with your friends" />
	  <area shape="rect" coords="676,40,728,67" href="http://www.pie.edu/login.php" target="_blank" alt="Log into pie.edu" />
	</map>

<map name="Map2" id="Map2">
  <area shape="rect" coords="624,56,664,92" href="http://www.facebook.com/pie.edu" target="_blank" alt="Follow us on Facebook" />
  <area shape="rect" coords="666,56,701,94" href="http://www.google.com/pie.edu" target="_blank" alt="Follow us on Google+" />
  <area shape="rect" coords="702,56,733,94" href="http://www.twitter.com/pie.edu" target="_blank" alt="Follow us on Twitter" />
</map>';

	//HTML Headers
	$cabeceras  = 'MIME-Version: 1.0' . "\r\n";
	$cabeceras .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

	//Additional Headers
	$cabeceras .= 'From: Account System <noreply@pie.edu>' . "\r\n";

	// Mail it
	mail($email, "Thanks for signin up to pie.edu! Your Education Network", $mensaje, $cabeceras);
	
	switch($type){
		case 1:
			header("Location: /a/first-login.php");
			exit;
			break;
		case 2:
			header("Location: /b/first-login.php");
			exit;
			break;
		case 3:
			header("Location: /c/first-login.php");
			exit;
			break;
	}


	
?>